<?php
class N_DB_Driver_MysqlPDO extends PDO 
{
	/**
	 * 数据库用户名
	 *
	 * @var string
	 */
	private $_user;
	
	/**
	 * 数据库连接密码
	 *
	 * @var string
	 */
	private $_password;
	
	public function __construct()
	{
		$dsn = N::getIni('DSN');
		if (is_array($dsn))
		{
			$dsn = $this->_parseDSN($dsn);
		}
		else if ($dsn != '')
		{
			$this->_user = N::getIni('user');
			$this->_password = N::getIni('password');
		}
		else 
		{
			throw new Exception("数据库连接参数错误!");
		}
		parent::__construct($dsn, $this->_user, $this->_password, array(PDO::ERRMODE_EXCEPTION=>true));
		unset($this->_user);
		unset($this->_password);
		parent::query('SET NAMES UTF8');
	}
	
	public function getErrorMsg()
	{
		$tempArray = $this->errorInfo();
		return $tempArray[1].':'.$tempArray[2];
	}
	
	public function selectDb($dbName)
	{
		$rtn = $this->query('use '.$dbName);
	}
	
	private function _parseDSN($dsnArray)
	{
		$dbType = null;
		$host = null;
		$db = null;
		if (!empty($dsnArray['host']))
		{
			$host = $dsnArray['host'];
		}
		if (!empty($dsnArray['db']))
		{
			$db = $dsnArray['db'];
		}
		if (!empty($dsnArray['user']))
		{
			$this->_user = $dsnArray['user'];
		}
		if (!empty($dsnArray['password']))
		{
			$this->_password = $dsnArray['password'];
		}
		if (strtolower($dbType) == 'mysql')
		{
			return 'mysql:dbname='.$db.';host='.$host;
		}
	}
}	